﻿using PIMS.WorkPlugin.ORM;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace PIMS.WorkPlugin
{
    public partial class Form产品型号编辑 : Form
    {
        private bool isEdit = false;
        private int uid = 0;

        public Form产品型号编辑()
        {
            InitializeComponent();
            this.Text = "新增产品型号";
        }
        public Form产品型号编辑(int id)
        {
            InitializeComponent();
            this.Text = "编辑产品型号";
            this.uid = id;
            this.isEdit = true;
        }

        private void Form产品型号编辑_Load(object sender, EventArgs e)
        {
            if (this.isEdit && this.uid > 0)
            {
                try
                {
                    string cmd = "select * from 产品型号定义表 where ID=" + this.uid + "";
                    DataTable dt = SqlSugarHelper.QueryToDataTable<ProductType>(cmd);
                    if (dt != null && dt.Rows.Count > 0)
                    {
                        if (dt.Rows[0]["使能"].ToString() == "1")
                            this.是否生效.Checked=true;
                        else
                            this.是否生效.Checked = false;
                        型号.Text = dt.Rows[0]["型号"].ToString();
                        粒径.Text = dt.Rows[0]["粒径"].ToString();
                        备注.Text = dt.Rows[0]["备注"].ToString();
                        txtMCode.Text = dt.Rows[0]["物料编码"].ToString();

                        int mid;
                        int.TryParse(dt.Rows[0]["模板ID"].ToString(), out mid);
                        this.模板ID.Value = mid;
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, "Form增加用户->Form增加用户_Load");
                }
            }
        }

        private void btn_Ack_Click(object sender, EventArgs e)
        {
            try
            {
                this.Err.Clear();
                if (String.IsNullOrEmpty(型号.Text))
                {
                    this.Err.SetError(this.型号, "禁止为空！");
                    return;
                }
                if (!isUniqueName(型号.Text))
                {
                    this.Err.SetError(this.型号, "型号名已使用！");
                    return;
                }
                if (String.IsNullOrEmpty(粒径.Text))
                {
                    this.Err.SetError(this.粒径, "禁止为空！");
                    return;
                }
                if (String.IsNullOrEmpty(txtMCode.Text))
                {
                    this.Err.SetError(this.txtMCode, "禁止为空！");
                    return;
                }
                if (模板ID.Value>0 && !isUniqueModeID((int)模板ID.Value))
                {
                    this.Err.SetError(this.模板ID, "模板ID已使用！");
                    return;
                }               

                string state = "0";
                if (this.是否生效.Checked) state = "1";               

                string cmdSql = "";
                if (!this.isEdit)
                {
                    ProductType pt = new ProductType();
                    pt.ID = 0;
                    pt.name = 型号.Text;
                    pt.size = 粒径.Text;
                    pt.code = txtMCode.Text;
                    pt.enable = state;
                    pt.modeId = (int)模板ID.Value;
                    pt.mark = 备注.Text;
                    pt.time = DateTime.Now;

                    if (SqlSugarHelper.InsertRecord<ProductType>(pt) > 0)
                    {
                        MessageBox.Show("增加新产品成功！", "成功", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                    }
                    else
                    {
                        MessageBox.Show("增加新产品失败！", "出错", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                }
                else
                {
                    ProductType pt = new ProductType();
                    pt.ID = this.uid;
                    pt.name = 型号.Text;
                    pt.size = 粒径.Text;
                    pt.code = txtMCode.Text;
                    pt.enable = state;
                    pt.modeId = (int)模板ID.Value;
                    pt.mark = 备注.Text;
                    pt.time = DateTime.Now;

                    if (SqlSugarHelper.UpdateRecord<ProductType>(pt) > 0)
                    {
                        MessageBox.Show("编辑产品成功！", "成功", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                    }
                    else
                    {
                        MessageBox.Show("编辑产品失败！", "出错", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                }

                this.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Form产品型号编辑->btn_Ack_Click");
            }
        }

        private void btn_Cancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        private bool isUniqueName(string name)
        {
            string cmd = "select * from 产品型号定义表 where 型号='" + name + "'";
            DataTable dt = SqlSugarHelper.QueryToDataTable<ProductType>(cmd);
            if (dt != null && dt.Rows.Count > 0)
            {
                if (this.isEdit && dt.Rows[0]["ID"].ToString() == this.uid.ToString())
                {
                    return true;
                }
                return false;
            }
            return true;
        }

        private bool isUniqueModeID(int mid)
        {
            string cmd = "select * from 产品型号定义表 where 模板ID='" + mid.ToString() + "'";
            DataTable dt = SqlSugarHelper.QueryToDataTable<ProductType>(cmd);
            if (dt != null && dt.Rows.Count > 0)
            {
                if (this.isEdit && dt.Rows[0]["ID"].ToString() == this.uid.ToString())
                {
                    return true;
                }
                return false;
            }
            return true;
        }
    }
}
